/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2025-2025. All rights reserved.
 *
 */
import * as path from 'path';

import { describe, expect, test } from '@jest/globals';
import * as fse from 'fs-extra';

import { initByStageEmptyApp, initExecuteCommand, runHvigor } from './common/common';

describe('Compatible test', () => {
  /**
   * sdk tsc SyntaxKind 接口兼容性测试。 noEmitJs 新方案需要读取 TS AST 结点类型，所以依赖 sdk tsc 中的 SyntaxKind接口，
   * 为了保证兼容性，这里增加e2e测试，测试 SyntaxKind 接口是否改变。
   */
  test.concurrent('tsc SyntaxKind compatible test', async () => {
    await initByStageEmptyApp(async (dir) => {
      const commands = initExecuteCommand({
        mode: 'module',
        product: 'default',
        tasks: 'assembleHap',
      });
      const result = runHvigor(dir, commands);
      expect(result.stdout.includes('BUILD SUCCESSFUL')).toBeTruthy();
      expect(result.stdout).toContain('etsLoaderPath: ');

      const pathMatch = result.stdout.match(/etsLoaderPath: '([^']+)',/);
      if (pathMatch && pathMatch[1]) {
        const etsLoaderPath = pathMatch[1];
        expect(fse.existsSync(etsLoaderPath)).toBe(true);
        const ts = require(path.join(etsLoaderPath, 'node_modules', 'typescript'));
        expect(ts !== undefined).toBe(true);
        expect(ts.SyntaxKind !== undefined).toBe(true);
        const props =
          ts.SyntaxKind.ImportDeclaration &&
          ts.SyntaxKind.ImportClause &&
          ts.SyntaxKind.NamedImports &&
          ts.SyntaxKind.NamespaceImport &&
          ts.SyntaxKind.ImportSpecifier &&
          ts.SyntaxKind.ExportDeclaration &&
          ts.SyntaxKind.NamedExports &&
          ts.SyntaxKind.NamespaceExport &&
          ts.SyntaxKind.CallExpression &&
          ts.SyntaxKind.ImportKeyword &&
          ts.SyntaxKind.ImportEqualsDeclaration &&
          ts.SyntaxKind.Identifier &&
          ts.SyntaxKind.StringLiteral &&
          ts.SyntaxKind.ExportSpecifier;

        expect(props !== undefined).toBe(true);
      }
    });
  });
});
